package com.tgy.helloworld.PostConstructTest;

import com.tgy.helloworld.PostConstructTest.Interface.Hello;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/***
 * @ClassName: Test
 * @Description: 测试Java 注解PostConstruct
 * @Auther: tanggy3
 * @Date: 14:20 2022/5/16
 * @version : V1.0
 */
@Component
//让soring管理这个类
public class TestPostConstruct {
    @Resource
    private Hello hello;

    private static Hello helloStatic;


    //如果没有这个的话就会空指针异常
    @PostConstruct
    public void init(){
        this.helloStatic = this.hello;
        System.out.println("bean注入完成");
    }

    public static String say(){
        //因为是静态的，所以在这里不能使用注入的bean
        //hello;//报错
        return helloStatic.sayHello();
    }

}

